package my.sample.db.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import com.mysql.jdbc.PreparedStatement;

public class BatchPrepared {

	final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	Connection connection = null;
	
	public BatchPrepared() {
		try {
			Class.forName(JDBC_DRIVER);
			connection = DriverManager.getConnection("jdbc:mysql://119.226.225.226:3306/beaserv", "root", "password");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	
	public void exec() throws SQLException {
		Statement statement = null;
		ResultSet result = null;
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("a", "a");
		map.put("c", "c");
		map.put("b", "b");
		map.put("d", "d");
		
		String s = ("insert into beaser.user (nickname) values (?)");
		 PreparedStatement preparedStatement = (PreparedStatement) connection.prepareStatement(s);
	      for (String key : map.keySet())
	        {
	    	  System.out.println(key);
	           preparedStatement.setString(1, key);
	           System.out.println(preparedStatement);
	            preparedStatement.addBatch();
	        }

	      System.out.println(preparedStatement);
	      System.out.println(preparedStatement.getPreparedSql());
	      preparedStatement.executeBatch();
	      System.out.println(preparedStatement.toString());
		
		
/*		try {
			statement = this.connection.createStatement();
			result = statement.executeQuery("select device_backup from backup_analyzer_status where backup_date='2014-08-08'");
			while( result.next()) {
				System.out.println(result.getInt("analyze_id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}*/
		
		
	}


	public static void main(String[] args) throws SQLException {
		
		BatchPrepared insert = new BatchPrepared();
		insert.exec();
		
	}

}
